package cn.itcast.erp.service.impl;

import cn.itcast.erp.dao.mapper.RoleMapper;
import cn.itcast.erp.entity.*;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.service.RoleService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public AjaxResult readRoleMenus(Role role) {
        AjaxResult ajaxResult = new AjaxResult();
        int count = roleMapper.count();
        List<Role> roleList = roleMapper.findRole(role);

        ajaxResult.setTotal(count);
        ajaxResult.setRows(roleList);

        return ajaxResult;
    }

    @Override
    public List<Role> readRoleMenusAll(Role role) {
        return roleMapper.findRoleAll(role);
    }

    @Override
    public int updateRole(Role role) {

        return roleMapper.updateRole(role);
    }

    @Override
    public int deleteRole(Role role){
        return roleMapper.deleteRole(role);
    }

    @Override
    public Role findRoleById(Long uuid) {
        return this.roleMapper.findRoleById (uuid);
    }

    @Override
    public List<Tree> findRoles(Long uuid) {
        List<Tree> treeList = new ArrayList<Tree>();

        //获取用户下的角色列表
        List<String> roleName = roleMapper.searchEmpRoleSeting(uuid);

        //获取所有角色列表
        List<Role> allRoleList = roleMapper.findRoleAll(null);

        Tree t1 = null;
        for(Role role : allRoleList){
            t1 = new Tree();
            //转换成string类型
            t1.setId(String.valueOf(role.getUuid()));
            t1.setText(role.getName());
            //判断是否需要勾选中,用户是否拥有这个角色
            if(roleName.contains(role.getName())){
                t1.setChecked(true);
            }
            treeList.add(t1);
        }

        return treeList;
    }

    @Override
    public int insertRole(Role role){
        return roleMapper.insertRole(role);
    }


    @Transactional
    @Override
    public int  updateEmpRoles(Long uuid, String checkedStr) {

        int i1 = 0;
        int i = this.roleMapper.deleteEmpRole(uuid);
        List<EmpRole> empRoleList = new ArrayList();
        String[] ids = checkedStr.split(",");
        if (checkedStr.length() != 0) {
            for (String id : ids) {

                EmpRole empRole = new EmpRole();
                empRole.setEmpuuid(uuid);
                empRole.setRoleuuid(Long.valueOf(id));
                empRoleList.add(empRole);

//                i1 = this.roleMapper.insertEmpRole(uuid, Long.valueOf(id));
            }
            i1 = this.roleMapper.insertEmpRoleTwo(empRoleList);

        }

        if (i >= 0 && i1 >= 0){
            return 1;
        }else {
            return 0;
        }

    }


    // 导出Excel
    @Override
    public InputStream getAllRoleEx() {
        //添加数据
        List<Role> role = roleMapper.getAllRoleEx();

        HSSFWorkbook sheets = new HSSFWorkbook();  // 定义一个新的工作簿
        Sheet sheet = sheets.createSheet("角色设置信息");  // 创建第一个新的Sheet页

        //创建第一行（也可以成为表头）
        Row row1 = sheet.createRow(0);
        sheet.setColumnWidth(0, 20 * 256);
        sheet.setColumnWidth(1, 30 * 256);

        //给表头第一行一次创建单元格
        String[] headers = {"编号","名称"};
        for(int i = 0; i < headers.length; i++){
            row1.createCell(i).setCellValue(headers[i]);
            row1.setHeightInPoints(30);//设置行的高度是30
        }

        //向单元格里添加数据
        for(short i=0;i<role.size();i++){
            row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(role.get(i).getUuid());
            row1.createCell(1).setCellValue(role.get(i).getName());
            row1.setHeightInPoints(30);//设置行的高度是30
        }
        //字节流缓存区
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        try {
            sheets.write(bo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ByteArrayInputStream(bo.toByteArray());
    }


}
